package com.micai.springboot.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.micai.springboot.dto.UserDto;
import com.micai.springboot.entity.UserEntity;
import com.micai.springboot.service.UserService;
import com.micai.springboot.util.MessageUtils;
import com.micai.springboot.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 描述：用户管理
 * <p>
 * Author: 赵新国
 * Date: 2017/11/14 15:21
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询用户列表
     * @param model
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@RequestParam(value="pageNum", defaultValue="1") int pageNum,
                       @RequestParam(value="pageSize", defaultValue="5") int pageSize,
                       @RequestParam(required = false) String userName, Model model) {
        Page<UserEntity> userDtos = userService.getUserByPage(pageNum, pageSize, userName);
        if (userDtos == null && userDtos.getResult().isEmpty()) {
            return null;
        }
        PageInfo<UserEntity> page = new PageInfo<UserEntity>(userDtos);
        model.addAttribute("page",page);
        // 参数回显
        model.addAttribute("userName", userName);
        return "admin/user/userList";
    }

    /**
     * 用户列表
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public PageInfo<UserVo> getList(@RequestParam Integer pageNumber, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<UserDto> userDtos = userService.listUsers();
        if (userDtos == null) {
            return null;
        }
        List<UserVo> userVos = new ArrayList<UserVo>();
        for (UserDto userDto : userDtos) {
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userDto, userVo);
            userVos.add(userVo);
        }
        return new PageInfo<UserVo>(userVos);
    }

    /**
     * 保存用户信息
     *
     * @param userVo
     * @return
     */
    @PostMapping(value = "save")
    public HashMap<String, Object> save(@ModelAttribute UserVo userVo) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userVo, userDto);
            userService.save(userDto);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "get")
    public HashMap<String, Object> get(@PathVariable Long id, Model model) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            UserDto userDto = userService.getUserById(id);
            UserVo userVo = new UserVo();
            BeanUtils.copyProperties(userDto, userVo);
            model.addAttribute("userVo", userVo);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 修改用户信息
     *
     * @param userVo
     * @return
     */
    @PutMapping(value = "update")
    public HashMap<String, Object> update(@ModelAttribute UserVo userVo) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            UserDto userDto = new UserDto();
            BeanUtils.copyProperties(userVo, userDto);
            userService.update(userDto);
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

    /**
     * 根据ID删除用户信息
     *
     * @param id
     * @return
     */
    @PutMapping(value = "delete")
    public HashMap<String, Object> delete(@PathVariable Long id) {
        HashMap<String, Object> retMap = MessageUtils.success();
        try {
            UserDto userDto = userService.getUserById(id);
            if (userDto != null) {
                userService.delete(userDto);
            }
        } catch (Exception e) {
            retMap = MessageUtils.error();
        }
        return retMap;
    }

}
